package com.miao.hong.fang.util;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.miao.hong.fang.IConstants;

public class Utils {

	public static List getCellOfList(List originList, int num, String orderType) {
		
		if(originList.size() <= num) {
			return originList;
		}
		
		List<Object> list = new ArrayList<Object>();
		
		if (IConstants.ORDER_TYPE_RANDOM.equals(orderType)) {
			Random random = new Random();
			for(int i = 0; i < num; i++) {
				
				int j = random.nextInt(originList.size() - 1);
				list.add(originList.get(j));
				originList.remove(j);
			}			
		}else if (IConstants.ORDER_TYPE_FRONT.equals(orderType)) {
			list = originList.subList(0, num);
		}
		
		return list;
	}
	
	public static String parseZhCN(String str) throws UnsupportedEncodingException {
		if(str == null) {
			return "";
		}
		return java.net.URLDecoder.decode(str, "UTF-8");  
	}
	
	public static Timestamp stringToTimestamp(String timestampAsStr, String format) {

		if (timestampAsStr == null || timestampAsStr.trim().equals("")) {
			return null;
		}

		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		
		Date date = null;
		try{
			date = dateFormat.parse(timestampAsStr);		
		}catch(Exception e) {
			System.out.println(e.getMessage());
		}
		
		return new Timestamp(date.getTime());
	}
	
	public static String timestampToString(Timestamp time, String format) {

		if(time == null) {
			return "";
		}
		
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(time);
	}
	
	public static Timestamp addDays(Timestamp time, Integer amount) {
		
		if(time == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		c.add(5, amount.intValue() - 1);
		
		return new Timestamp(c.getTime().getTime());
	}
	
	public static Timestamp getCurrentDate() {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
		int day = Calendar.getInstance().get(Calendar.DATE);
		
		String monthStr = String.valueOf(month);
		if(month < 10) {
			monthStr = "0" + monthStr;
		}
		String dayStr = String.valueOf(day);
		if(day < 10) {
			dayStr = "0" + dayStr;	
		}
		
		String date = year + "-" + monthStr + "-" + dayStr;
		return stringToTimestamp(date, IConstants.COURSE_START_TIME_FORMAT);
	}
	
	public static void saveMessages(HttpServletRequest request, String msg) {
		
		ActionMessages messages = new ActionMessages();
		messages.add("success", new ActionMessage(msg));
		request.setAttribute(Globals.MESSAGE_KEY, messages);
	}
	
	public static void addToCache(String key, Object value) {
		CacheManager manager = CacheManager.create();
		Cache cache = manager.getCache("test");
		if(cache == null) {
			cache = new Cache("test", 1000, true, true, 120, 120);
			manager.addCache(cache);
		}
		
		//remove old cache first
		//...
		Element element = new Element(key, value);
		cache.put(element);
	}
	
	public static Object getFromCache(String key) {
		CacheManager manager = CacheManager.create();
		Cache cache = manager.getCache("test");
		
		return cache.get(key).getObjectValue();
	}
}
